<template>
	<!-- 打卡日历页面 -->
	<view class='all'>
		<view class="bar">
			<!-- 显示年月 -->
			<view class="date">{{cur_year || "--"}} - {{cur_month || "--"}} - {{today}}</view>
		</view>
		<!-- 显示星期 -->
		<view class="week">
			<view v-for="(item,index) in weeks_ch" :key="index">{{item}}</view>
		</view>
		<view class="myDateTable">
			<view v-for="(item,j) in days" :key="j" class='dateCell'>
				<view v-if="item.date==undefined||item.date == null" class='cell'>
					<text :decode="true">&nbsp;&nbsp;</text>
				</view>
				<view v-else>
					<!-- 已签到日期 -->
					<view v-if="item.isSign == true" class='cell greenColor bgWhite  '>
						<text>{{item.date}}</text>
					</view>
					<!-- 漏签 -->
					<view class="cell redColor"
						v-else-if="cur_year<toYear||(cur_year==toYear&&cur_month<toMonth)||(cur_year==toYear&&cur_month==toMonth&&item.date<today)">
						<!-- 小程序不兼容这个 v-else-if="(new Date(cur_year+'-'+cur_month+'-'+item.date))<(new Date())"> -->
						<text>{{item.date}}</text>
					</view>
					<!-- 今日未签到-->
					<view class="cell whiteColor " v-else-if="item.date==today&&cur_month==toMonth&&cur_year==toYear">
						<view class="aa bgBlue" :class="show?'active':''">{{item.date}}</view>
					</view>
					<!-- 当前日期之后 -->
					<view class="whiteColor cell" v-else>
						<text>{{item.date}}</text>
					</view>
				</view>

			</view>
		</view>
		<view class="submit-box">
			<view class="submit" @click="clickSignUp(1)" :class="show?'actives':''">{{show?'已签到':'签到'}}</view>
		</view>
		<view class="text">赶快签到吧！每次签到可得10积分</view>
		<u-popup v-model="shows" mode="center" border-radius="20" :closeable="false">
			<view class="img">
				<image src="" mode=""></image>
				<view class="img-box">
					<view class="h">签到成功</view>
					<view class="span">恭喜您获得 10 积分</view>
					<view class="btn">
						<view class="btn-box" @click="shows=!shows">
							我知道了
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				days: [],
				SignUp: [],
				cur_year: 0, //当前选的年
				cur_month: 0, //当前选的月
				today: parseInt(new Date().getDate()), //本日
				toMonth: parseInt(new Date().getMonth() + 1), //本月
				toYear: parseInt(new Date().getFullYear()), //本年
				weeks_ch: ['一', '二', '三', '四', '五', '六', '日'],
				show: false,
				shows: false,
			};
		},
		props: {
			sendYear: {
				type: Number,
				default: new Date().getFullYear()
			},
			sendMonth: {
				type: Number,
				default: new Date().getMonth() + 1
			},
			dataSource: { //已签到的数据源
				type: Array,
				default: () => {
					return []
				}
			},
			langType: { //只是示例一个翻译而已，要想所有都翻译自己可以再加加
				type: String,
				default: "ch"
			},
		},
		created() {
			this.cur_year = this.sendYear;
			this.cur_month = this.sendMonth;
			this.SignUp = this.dataSource;

			this.calculateEmptyGrids(this.cur_year, this.cur_month);
			this.calculateDays(this.cur_year, this.cur_month);
			this.onJudgeSign();
		},
		watch: {
			dataSource: 'onResChange',
		},
		methods: {
			// 获取当月共多少天
			getThisMonthDays(year, month) {
				return new Date(year, month, 0).getDate()
			},
			// 获取当月第一天星期几
			getFirstDayOfWeek(year, month) {
				return new Date(Date.UTC(year, month - 1, 1)).getDay();
			},
			// 计算当月1号前空了几个格子，把它填充在days数组的前面
			calculateEmptyGrids(year, month) {
				//计算每个月时要清零
				this.days = [];
				const firstDayOfWeek = this.getFirstDayOfWeek(year, month) - 1;
				if (firstDayOfWeek > 0) {
					for (let i = 0; i < firstDayOfWeek; i++) {
						var obj = {
							date: null,
							isSign: false
						}
						this.days.push(obj);
					}
				}
			},

			// 绘制当月天数占的格子，并把它放到days数组中
			calculateDays(year, month) {

				const thisMonthDays = this.getThisMonthDays(year, month);
				// this.columnsLen=Math.ceil(thisMonthDays/7);
				// console.log(this.columnsLen);
				for (let i = 1; i <= thisMonthDays; i++) {
					var obj = {
						date: i,
						isSign: false
					}
					this.days.push(obj);
				}
				//console.log(this.days);

			},

			onResChange(newD, oldD) {
				this.SignUp = newD;
				this.onJudgeSign();
			},
			//匹配判断当月与当月哪些日子签到打卡
			onJudgeSign() {
				var signs = this.SignUp;
				var daysArr = this.days;
				for (var i = 0; i < signs.length; i++) {
					var current = new Date(signs[i].replace(/-/g, "/"));
					var year = current.getFullYear();
					var month = current.getMonth() + 1;
					var day = current.getDate();
					day = parseInt(day);
					for (var j = 0; j < daysArr.length; j++) {
						//年月日相同则打卡成功   						
						if (year == this.cur_year && month == this.cur_month && daysArr[j].date ==
							day) { //&& signs[i].isSign == "今日已打卡"
							// console.log(daysArr[j].date, day);
							daysArr[j].isSign = true;
						}
					}
				}
				this.days = daysArr;
			},
			//签到
			clickSignUp(type) { //type=0补签，type=1当日签到
				if (this.show == false) {
					this.shows = true
					this.show = true

				} else
				if (this.show == true) {
					uni.showToast({
						title: '请勿重新签到',
						duration: 2000,
						icon:'none'
					});
					this.shows = false
				}
				// console.log(this.SignUp);
				// this.$forceUpdate();

				// this.$emit('clickChange', this.cur_year + "-" + this.cur_month + "-" + date); //传给调用模板页面

				//refresh
				this.onJudgeSign();

			}
		}
	}
</script>

<style lang="scss" scoped>
	.actives{
		background: linear-gradient(180deg, #39D490 0%, #00C16D 100%)!important;
	}
	.img {
		width: 600rpx;
		height: 654rpx;
		background: pink;
		position: relative;

		image {
			width: 100%;
			height: 100%;
		}

		.img-box {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;

			.h {
				font-size: $h2;
				font-weight: 500;
				color: #00B164;
				text-align: center;
				margin-bottom: 20rpx;
			}

			.span {
				font-size: $h5;
				font-weight: 400;
				color: #000000;
				text-align: center;
				margin-bottom: 40rpx;
			}

			.btn {
				line-height: 80rpx;
				font-size: $h4;
				font-weight: 400;
				color: #FFFFFF;
				-webkit-text-stroke: 1 rgba(0, 0, 0, 0);
				text-stroke: 1 rgba(0, 0, 0, 0);
				display: flex;
				justify-content: space-around;
				margin-bottom: 50rpx;
				.btn-box {
				border-radius: 40rpx;
				padding: 0 104rpx;
					background: linear-gradient(180deg, #39D490 0%, #00C16D 100%);
				}
			}
		}
	}

	.text {
		font-size: $h5;
		font-weight: 400;
		color: #808080;
		text-align: center;
		padding-bottom: 55rpx;
	}

	.submit-box {
		display: flex;
		justify-content: space-around;
		margin-top: 50rpx;
		padding-bottom: $box-space-big;

		.submit {
			line-height: 80rpx;
			background: linear-gradient(90deg, #FFBF71 0%, #FF5507 100%);
			border-radius: 40rpx;
			padding: 0 121rpx;
			font-size: $h4;
			font-weight: 400;
			color: #FFFFFF;
		}
	}

	.date {
		font-size: $h4;
		font-weight: 500;
		color: #000000;
	}

	.aa {
		width: 42rpx;
		height: 42rpx;
		border-radius: 50%;
	}

	.active {
		background: linear-gradient(180deg, #39D490 0%, #00C16D 100%) !important;
	}

	.all {
		margin-top: 20rpx;
	}

	.all .bar {
		display: flex;
		justify-content: space-evenly;
		/* margin-bottom: 60rpx; */
		padding: $box-space-big 0;
		margin: 0 50rpx;
		border-bottom: 1rpx solid #F5F5F5;
	}

	.all .week {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 30rpx 10rpx 0 10rpx;
		margin: auto;
		border-radius: 10px;
		background-color: #fff;
		font-size: $h4;
		font-weight: 500;
		color: #000000;
		width: 96%;

		view {
			width: 14%;
			text-align: center;
		}
	}

	.myDateTable {
		padding: 0 10rpx;
		margin: auto;
		// padding: 2vw;
		border-radius: 10px;
		background: #FFFFFF;
		width: 98%;
	}

	.myDateTable .dateCell {
		width: 14%;
		/* padding: 1vw; */
		display: inline-block;
		text-align: center;
		font-size: 16px;
	}

	.dateCell .cell {
		display: flex;
		border-radius: 50%;
		height: 8vw;
		justify-content: center;
		align-items: center;
	}


	.whiteColor {
		color: #000000;
	}

	.greenColor {
		color: #01b90b;
		font-weight: bold;
	}

	.bgWhite {
		background-color: #fff;
	}

	.bgBlue {
		font-size: 14px;
		background-color: #FF5A0C;
		color: #FFFFFF;
	}

	.redColor {
		color: #00B164;
	}

	.impTip {
		display: inline-block;
		color: #ff0000;
	}
</style>
